﻿using habbo.Kernel.Commons;
using habbo.Kernel.Storage.Querys;
using habbo.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Habbo.Catalog
{
    class CatalogManager
    {
        /// <summary>
        /// BluedotDictionary ; Loads all pages
        /// </summary>
        public BluedotDictionary<int, CatalogPage> WeakPageCache;

        /// <summary>
        /// BluedotDictionary ; Loads all pages
        /// </summary>
        public BluedotDictionary<int, CatalogPage> WeakSubPagesCache;

        /// <summary>
        /// BluedotDictionary ; Loads all pages
        /// </summary>
        public BluedotDictionary<int, CatalogPage> WeakAllPagesCache;

        /// <summary>
        /// Initializes CatalogManager
        /// </summary>
        public CatalogManager()
        {
            WeakPageCache = new BluedotDictionary<int, CatalogPage>();
            WeakSubPagesCache = new BluedotDictionary<int, CatalogPage>();
            WeakAllPagesCache = new BluedotDictionary<int, CatalogPage>();
        }




        public CatalogPage GetPage(int PageId)
        {
            CatalogPage CatalogPage;

            if (!WeakPageCache.TryGetValue(PageId, out CatalogPage))
            {
                CatalogPage = new CatalogPage(SystemApp.MySQLManager.GetObject(new GetCatalogPage(PageId)).GetOutput<DataRow>());
                WeakPageCache.Add(PageId, CatalogPage);
            }

            return CatalogPage;
        }

        public bool GetPage(int PageId, out CatalogPage CatalogPage)
        {
            CatalogPage = GetPage(PageId);

            return CatalogPage != null;
        }

        public ICollection<CatalogPage> GetPages()
        {
            ICollection<CatalogPage> Output = new List<CatalogPage>();

            foreach (DataRow Row in SystemApp.MySQLManager.GetObject(new SelectAllCatalogPages()).GetOutput<DataTable>().Rows)
            {
                using (RowAdapter Adapter = new RowAdapter(Row))
                {
                    int PageId = Adapter.PopInt32("id");
                    CatalogPage Page = new CatalogPage(Row);

                    if (!WeakAllPagesCache.ContainsKey(PageId))
                    {
                        WeakAllPagesCache.Add(PageId, Page);
                    }

                    foreach (DataRow Rows in SystemApp.MySQLManager.GetObject(new GetSubCategories(Page.Id)).GetOutput<DataTable>().Rows)
                    {
                        using (RowAdapter Adapting = new RowAdapter(Rows))
                        {
                            int pageId = Adapting.PopInt32("id");
                            CatalogPage Paging = new CatalogPage(Rows);

                            if (!WeakSubPagesCache.ContainsKey(pageId))
                            {
                                WeakSubPagesCache.Add(pageId, Paging);
                            }
                            
                        }
                    }

                    Output.Add(Page);
                }
            }

            return Output;
        }

        public ICollection<CatalogPage> GetSubPages(int pageId)
        {
            ICollection<CatalogPage> Output = new List<CatalogPage>();

            foreach (CatalogPage Page in WeakSubPagesCache.Values)
            {
                if (Page.ParentId == pageId)
                {
                    Output.Add(Page);
                }
            }

            return Output;
        }

    }
}
